#  Gmof - A meta-object facility library based on OMG's EMOF 
#
#  Copyright (C) 2007 Peter Hagg <peter.hagg@iki.fi>
#
#  This library is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2.1 of the License, or (at your option) any later version.
#
#  This library is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the Free Software
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
import unittest
from gmof.Package import Package
from gmof.Factory import Factory
from gmof.String import String
from gmof.Class import Class
from gmof.Property import Property

class FactoryTest(unittest.TestCase):
    
    def setUp(self):
        self.package = Package("Test", "com.google.code.gmof.test")
        self.factory = Factory(self.package)
        
    def tearDown(self):
        del self.package
        del self.factory
        
    def testCreation(self):
        """Test that the Factory gets properly created and initialized"""
        self.assertTrue(self.factory != None)
        self.assertEquals(self.package, self.factory.package)
        
    def testCreateFromString(self):
        """Test type creation using Factory.createFromString"""
        str = String("String", "")
        str.package = self.package
        str2 = self.factory.createFromString(str, "value")
        
        self.assertEquals("String", str2.name)
        self.assertEquals("value", str2.value)
        self.assertTrue(str2.isInstance(str))
        self.assertFalse(str.isInstance(str2))

    def testCreate(self):
        """Test meta class instance creation using Factory.create"""
        str = String("String", "")
        str.package = self.package
        
        person = Class("Person")
        person.package = self.package
        
        firstName = Property("firstName", str)
        person.ownedAttribute.append(firstName)
        
        lastName = Property("lastName", str)
        person.ownedAttribute.append(lastName)
        
        instance = self.factory.create(person)
        
        self.assertTrue(instance != None)
        self.assertEquals(self.package, instance.package)
        self.assertEquals(2, len(instance.ownedAttribute))
        self.assertEquals("Person", instance.name)
        self.assertEquals(person, instance.metaClass)
        
        instance.set(firstName, "Peter")
        value = instance.get(firstName)
        
        self.assertTrue(value != None)
        self.assertEquals("Peter", value)
        
